package com.zhan.ielts.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.CompositionJjForecast;
import com.zhan.ielts.domain.query.CompositionJjForecastQuery;
import com.zhan.ielts.domain.vo.CompositionJjForecastVo;
import com.zhan.ielts.service.CompositionJjForecastService;
import com.zhan.ielts.utils.CustomPropertisUtils;

@Controller
@RequestMapping("/composition/forecast")
public class CompositionJjForecastController extends
		BaseControllerImpl<CompositionJjForecast, CompositionJjForecastQuery> {

	private Logger log = LoggerFactory.getLogger(CompositionJjForecastController.class);

	@Autowired
	private CompositionJjForecastService compositionJjForecastService;

	@Override
	protected BaseService<CompositionJjForecast> getBaseService() {
		return compositionJjForecastService;
	}

	@SuppressWarnings({ "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchCompositionJjForecast(String userId, long updateTime) {
		log.debug("获取写作机经预测开始");

		// 检查userId 是否为空
		if (StringUtils.isEmpty(userId)) {
		}

		Map map = new HashMap();
		try {
			CompositionJjForecastVo compositionJjForecast = new CompositionJjForecastVo();
			compositionJjForecast.setUpdateTime(updateTime);
			Sort sort = new Sort(Direction.DESC, "updateTime").and(new Sort(Sort.Direction.DESC, "id"));
			Pageable pageable = new PageRequest(0, 100, sort);

			map = compositionJjForecastService.fetchCompositionJjForecast(compositionJjForecast, pageable);
			log.debug("获取写作机经预测结束");
		} catch (Exception e) {
			e.printStackTrace();
			return new CustomResult(0, "网络或系统错误！", map);
		}

		log.debug("获取写作机经预测结束");
		return new CustomResult(1, "", map);

	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
		log.debug("写作机经预测开始！");
		String fileFolder = CustomPropertisUtils.getProperties("filePath");
		boolean flag = true;
		if (file.isEmpty()) {
			return "excel can not be null.";
		}

		flag = compositionJjForecastService.updateCompositionJjForecast(file, fileFolder);

		if (!flag) {
			return "fail to update excel.";
		}

		log.debug("写作机经预测结束！");
		return "success!";
	}

	@ResponseBody
	@RequestMapping(value = "/getDate", method = RequestMethod.GET)
	public CustomResult getJjDate() {
		log.debug("获取机经日期开始");

		Map<String,String> map = new HashMap<String,String>();
		try {
			map = compositionJjForecastService.getJjDate();
			System.out.println("speaking------------"+map.get("speaking")+"------writing:"+map.get("writing"));
			log.debug("获取写作机经预测结束");
		} catch (Exception e) {
			e.printStackTrace();
			return new CustomResult(0, "网络或系统错误！", map);
		}

		log.debug("获取机经日期结束");
		return new CustomResult(1, "", map);

	}
}
